package com.example.mq.mqsever.datacenter;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-08-26
 * Time: 16:46
 */

import com.example.mq.MqApplication;
import com.example.mq.mqsever.core.Binding;
import com.example.mq.mqsever.core.Exchange;
import com.example.mq.mqsever.core.Exchangetype;
import com.example.mq.mqsever.core.MSGQueue;
import com.example.mq.mqsever.mapper.MetaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.io.File;
import java.nio.file.FileAlreadyExistsException;
import java.util.List;

/**
 * 这个类就是管理数据库里面的数据的.整合数据库的操作
 */
public class DataBaseManager {
    //从Spring拿到现成的对象

    private MetaMapper metaMapper;
    //针对数据库进行初始化,就是建库建表,并且插入一些默认数据(涉及到业务,所以单独拎出来更好)
    public void init(){
//手动获取MetaMapper对象
        //没有这个操作,后来调用metaMapper对象的时候可能会有空指针异常
        metaMapper= MqApplication.context.getBean(MetaMapper.class);

        //判定数据库是否存在
        if(!checkDBExists()){
            //不存在就建库建表
            File dataDir=new File("./data");
            dataDir.mkdirs();

            //创建数据表
            createDefaultTable();
            //插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManager]数据库初始化完成!");
        }else{
            //数据库已经存在,不用做啥了
            System.out.println("[DataBaseManager]数据库已经存在!");
        }


    }
    public  void deleteDB(){
        File file=new File("./data/meta.db");
     boolean ret=   file.delete();
     if(ret){
         System.out.println("[DataBaseManager]删除数据库文件成功");
     }else{
         System.out.println("[DataBaseManager]删除数据库文件失败");
     }
     File dataDir=new File("./data");
     //使用delete删除目录的时候,需要保证目录是空的
      ret=  dataDir.delete();
      if(ret){
          System.out.println("[DataBaseManager]删除数据库目录成功");
      }else{
          System.out.println("[DataBaseManager]删除数据库目录失败");
      }
    }


    private boolean checkDBExists() {
        File file=new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }
    //这个方法用来建表
    //建库不需要手动创建meta.db,首次执行数据库操作的时候,会自动创建mta.db文件(Mybatis完成的)
    private void createDefaultTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager]创建表完成");
    }
    //给数据库添加默认数据
    //主要是添加一个默认的交换机
    //RabbitMQ有这样一个设定,就是带有一个匿名的交换机.类型是Direct
    private void createDefaultData() {
        Exchange exchange=new Exchange();
        exchange.setName("");
        exchange.setType(Exchangetype.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);

    }
//把其他的数据库的操作,也在类中封装一下
    public void  insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }
    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }
    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }
    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }
    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }
    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }
    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }
    public  void deleteBiding(Binding binding){
        metaMapper.deleteBinding(binding);
    }

}
